import { createRouter, createWebHistory } from 'vue-router'
import { showNotify } from 'vant'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home',
    },
    // 首页
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/Home/index.vue'),
    },
    // 登录
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login/LoginView.vue'),
    },
    // 公告
    {
      path: '/notice',
      name: 'notice',
      component: () => import('../views/Notice/NoticeView.vue'),
    },
    // 路况
    {
      path: '/road',
      name: 'road',
      component: () => import('../views/Report/ReportView.vue'),
    },
    // 我
    {
      path: '/user',
      name: 'user',
      component: () => import('../views/User/User.vue'),
    },
    // 404
    {
      path: '/:pathMatch(.*)*',
      name: 'notFound',
      component: () => import('../views/NotFound/NotFound.vue'),
    },
  ],
})

// 前置导航守卫
router.beforeEach((to, from, next) => {
  //从localStorage判断用户是否登录过,若未登录直接导航到登录页面
  let isLogin = localStorage.getItem('isLogin')
  if (isLogin) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      showNotify({ message: '请先登录!!!', type: 'danger', duration: 2000 })
      next('/login')
    }
  }
})
export default router